# -*- coding: utf-8 -*-

import unittest

import os
import sys
import logging
import json
import time

path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, path)

from contract_market.initialize import config # NOQA
from contract_market.initialize.config import *  # NOQA
from contract_market.initialize.log import InitLogConfig # NOQA
from contract_market.strategy.conf import DynamicAllocation # NOQA
from contract_market.strategy.image import constant # NOQA
from contract_market.initialize.redis import OPRedis # NOQA

class Argv:
    process_name = ""
    symbol_name = ""

# 模拟修改里面的数据
def change_cache():

    key = "testConf:dot_usdt"
    client = OPRedis()
    cache = client.get(key)
    data = json.loads(cache.decode("utf8"))
    data["singel_order"]["value"] = 10000000000000000000
    cache = json.dumps(data)
    client.set(key, cache)

class TestConfigure(unittest.TestCase):

    @unittest.skip
    def test_init(self):
        lc = InitConfig("image", "pair.yaml")
        print("[resolve]", lc.resolve())
        pass

    @unittest.skip
    def test_log(self):
        InitLogConfig()
        logger = logging.getLogger("simpleExample")
        logger.info("dddddd")
        pass

    @unittest.skip
    def test_cache(self):

        Argv.process_name = "testConf"
        Argv.symbol_name = "dot_usdt"
        lc = InitConfig("image", "pair.yaml", argv=Argv)
        print("[resolve]", lc.resolve())
        dac = DynamicAllocation(lc, constant)
        pass

    def test_dynamic_cache(self):

        Argv.process_name = "testConf"
        Argv.symbol_name = "dot_usdt"

        lc = InitConfig("image", "pair.yaml", argv=Argv)

        dac = DynamicAllocation(lc, constant) # TOOD
        print("[origin] dac ", dac.jsonObj)

        time.sleep(2) # 模拟程序运行时间
        change_cache() # 修改配置
        time.sleep(11) # 模拟程序运行时间以及运行周期
        print("[change] dac ", dac.jsonObj)
        pass

if __name__ == "__main__":

    unittest.main()
    pass